package com.tianji.learning.job;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.enums.LessonStatus;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 写一个定时器：
 * 1. 先在启动类上开启任务调度
 * 2. 方法上设置多长时间执行
 * 3. 业务逻辑
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LessonStatusCheckJob {

    private final ILearningLessonService lessonService;

    @Scheduled(cron = "0 * * * * ?") // 每分钟执行一次
    public void lessonStatusCheck() {
        // 1. 查询状态为"未过期"的课程(不区分用户)
        List<LearningLesson> list = lessonService.list(Wrappers
                .<LearningLesson>lambdaQuery()
                .ne(LearningLesson::getStatus, LessonStatus.EXPIRED)
        );

        // 2. 判断是否过期
        LocalDateTime now = LocalDateTime.now();
        for (LearningLesson lesson : list) {
            if (now.isAfter(lesson.getExpireTime())) {
                lesson.setStatus(LessonStatus.EXPIRED);
            }
        }

        // 3. 批量更新
        lessonService.updateBatchById(list);

    }

}
